/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui.renderer;

import com.anhndt.audiobook.business.CustomBorder;
import com.anhndt.audiobook.business.Resource;
import com.anhndt.audiobook.ui.RateControl;
import com.anhndt.audiobook.entity.ItemEntity;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.UIManager;

/**
 *
 * @author Sun
 */
public class ItemRenderer extends Container implements ListCellRenderer {

    public static final byte TYPE_RATE = 0;
    public static final byte TYPE_ONE = 1;
    public static final byte TYPE_TWO = 2;
    protected byte type = TYPE_RATE;
    protected Label thumb, expand, lblName, lblDescription;
    protected RateControl rate;
//    public static Image iconDefault = Util.getImageAsResource("logo");
    public Image iconDefault = Util.getImageAsResource("logo");
    int bgUnselectedColor, bgSelectedColor;

    /**
     *       *****************************
     *       *          Info1            *
     *       *  Image                  > *
     *       *          Info2            *
     *       *****************************
     * @author Sun
     */
    public ItemRenderer(byte type) {
        setLayout(new BorderLayout());
        this.type = type;
        setUIID("ListRenderer");
        bgUnselectedColor = getUnselectedStyle().getBgColor();
        setFocusable(false);

        lblName = new Label("Unknown");
        lblName.setUIID("Name");
        if (type == TYPE_RATE || type == TYPE_TWO) {
            lblDescription = new Label();
            lblDescription.setUIID("Description");
        }
        if (type == TYPE_RATE) {
            rate = new RateControl();
            rate.setFocusable(false);
        }

        addingComponent();
        setPreferredW(Display.getInstance().getDisplayWidth());
        int color = UIManager.getInstance().getComponentStyle("Line").getBgColor();
        getUnselectedStyle().setBorder(new CustomBorder(color, 1, CustomBorder.BOTTOM));
        getSelectedStyle().setBorder(getUnselectedStyle().getBorder());
    }

    protected void addingComponent() {
        addingComponent(this);
    }

    protected void addingComponent(Container container) {
        //add component
        if (lblDescription != null) {
            Container panelInfo = new Container(new GridLayout(2, 1));
            Container panelDescription = new Container(new BorderLayout());
            panelDescription.addComponent(BorderLayout.CENTER, lblDescription);
            if (rate != null) {
                panelDescription.addComponent(BorderLayout.EAST, rate);
            }

            panelInfo.addComponent(lblName);
            panelInfo.addComponent(panelDescription);
            container.addComponent(BorderLayout.CENTER, panelInfo);
        } else {
            container.addComponent(BorderLayout.CENTER, lblName);
        }
    }

    public Label getTicker() {
        return lblName;
    }

    protected void createThumb() {
        if (thumb == null) {
            thumb = new Label(iconDefault);
            thumb.setUIID("Thumbnail");
        }
    }

    protected void addThumb() {
        this.addComponent(BorderLayout.WEST, thumb);
    }

    public Component getFocusComponent(Component list) {
        return this;
    }

    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
        if (value == null) {
            return this;
        }
        setFocus(isSelected);
//        if (index % 2 == 0) {
//            getUnselectedStyle().setBgColor(bgUnselectedColor);
//        } else {
//            getUnselectedStyle().setBgColor(0x293136);
//        }

        ItemEntity item = (ItemEntity) value;
        if (item.isMarked()) {
            int fgColor = getSelectedStyle().getFgColor();
            lblName.getUnselectedStyle().setFgColor(fgColor);
            if (lblDescription != null) {
                lblDescription.getUnselectedStyle().setFgColor(fgColor);
            }
        } else {
            boolean isSelectedInfo = (list != null && list.getComponentForm().getFocused() == list) ? isSelected : false;
            int fgColorName = UIManager.getInstance().getComponentStyle("Name").getFgColor();
            lblName.getUnselectedStyle().setFgColor(fgColorName);
            lblName.setFocus(isSelectedInfo);
            if (lblDescription != null) {
                int fgColorDescription = UIManager.getInstance().getComponentStyle("Description").getFgColor();
                lblDescription.getUnselectedStyle().setFgColor(fgColorDescription);
                lblDescription.setFocus(isSelectedInfo);
            }
        }
        if (rate != null) {
            rate.setRate(item.getRate());
        }
        String info1 = item.name;
        String info2 = item.description;
        Image icon = (item.icon != null) ? item.icon : iconDefault;
        lblName.setText(info1);
        if (lblDescription != null) {
            lblDescription.setText(info2);
        }

        if (thumb == null && item.hasThumbnail() && Util.displayThumb) {
            createThumb();
            addThumb();
        }

        if (thumb != null) {
            thumb.setIcon(icon);
        }
        if (item.hasExpandIcon()) {
            if (expand == null) {
                expand = new Label(Util.getImage("expand"));
                expand.getUnselectedStyle().setBgTransparency(Resource.TRANSPARENCE);
                this.addComponent(BorderLayout.EAST, expand);
            } else {
                expand.setVisible(true);
            }
        } else if (expand != null && item.typeEntity == ItemEntity.TYPE_CATEGORY) {
            expand.setVisible(false);
        }
        repaint();
        return this;
    }

    public Component getListFocusComponent(List list) {
        return null;
    }
}
